package com.android.qjpmc.application_module.activity;

import android.text.TextUtils;
import android.widget.Toast;

import com.android.qjpmc.R;
import com.android.qjpmc.bean.LoginInfoBean;
import com.android.qjpmc.bean.UserInfo;
import com.android.qjpmc.data_module.CUserModule;
import com.android.qjpmc.data_module.PwrDataModule;
import com.android.qjpmc.databinding.ActivityLoginBinding;
import com.android.qjpmc.manager.AccountManager;
import com.android.qjpmc.manager.IntentManager;
import com.android.qjpmc.widget.base.BaseCommonActivity;
import com.android.qjpmc.widget.bean.BaseRequestData;
import com.android.qjpmc.widget.manager.HttpsManager;
import com.android.qjpmc.widget.manager.RxJavaHttpManager;
import com.android.qjpmc.widget.manager.SPManager;
import com.android.qjpmc.widget.manager.retrofit.RetrofitJsonManager;
import com.android.qjpmc.widget.retrofithelper.rxschedulers.RxSchedulersHelper;
import com.android.qjpmc.widget.retrofithelper.rxsubscriber.ProgressObserver;
import com.android.qjpmc.widget.utils.ToastUtils;

import java.util.HashMap;
import java.util.Map;

public class LoginActivity extends BaseCommonActivity<ActivityLoginBinding> {
    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void initView() {
        super.initView();
        mBinding.setView(this);


    }

    public void onLogin() {
        String userName = mBinding.etUserName.getText().toString();
        if (TextUtils.isEmpty(userName)) {
            //ToastUtils.showShort("请输入用户名");
            Toast.makeText(this, "请输入用户名", Toast.LENGTH_SHORT).show();
            return;
        }
        String password = mBinding.etPassword.getText().toString();
        if (TextUtils.isEmpty(password)) {
            Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
            return;
        }
        CUserModule the_useer_module = PwrDataModule.getInstance().GetUserModule();
        if (the_useer_module == null) {
            Toast.makeText(this, "登录失败!", Toast.LENGTH_SHORT).show();
            return;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("username", userName);
        map.put("password", password);
        RetrofitJsonManager.getInstance().getApiService().login(map)
                .compose(RxSchedulersHelper.applyIoTransformer())
                .subscribe(new ProgressObserver<BaseRequestData<Object>>() {
                    @Override
                    public void _onNext(BaseRequestData<Object> loginInfoBean) {
                        if (loginInfoBean.statusCode == 200) {
                            the_useer_module.UserLogin(userName, password);
                            SPManager.LoginInfo.saveLoginInfo(userName);
                            AccountManager.getInstance().saveLoginData(LoginActivity.this, new LoginInfoBean());
                            IntentManager.toHomeActivity(LoginActivity.this);
                        }
                    }

                    @Override
                    public void _onCompleted() {
                        super._onCompleted();

                    }
                });

    }

    public void onSwitchApi() {
        HttpsManager.onSwitchApi(this);

    }

}